package com.kugong.signUp;

//~--- non-JDK imports --------------------------------------------------------

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import com.kugong.match.Match;
import com.kugong.user.User;

//~--- JDK imports ------------------------------------------------------------

import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2015/8/2.
 */
public class SignUpController extends Controller {

    /**
     * @date 15/08/06
     * @author liuxin
     */
    public void index() {
        String para = getPara();
        int page = 1;
        String matchId = "1";

        if (para != null) {
            matchId = getPara(0);
            page = getParaToInt(1);
        }

        Match match = Match.dao.findById(matchId);
        Page<SignUp> user = SignUp.dao.paginate(page, 20, matchId);

        setAttr("match", match);
        setAttr("users", user);
        render("index.jsp");
    }

    /**
     * @date 15/08/06
     * @author liuxin
     */
    public void signUp() {
        String matchId = getPara();
        String signName = getSessionAttr("signName");

        Match match = new Match();
        match = match.findById(matchId);
        Date beg = match.getDate("signUpTime");
        Date end = match.getDate("closingTime");
        Date now = new Date();
        if (now.after(end) || now.before(beg)) {
            renderText("报名时间未开始或已结束，感谢您的关注。");
        } else {
            if (signName != null && !signName.trim().equals("")) {
                SignUp users = SignUp.dao.qrySignUpByUserIdAndMatchId(getSessionAttr("userId").toString(), matchId);

                if (users!=null) {
                    render("success.jsp");
                } else {
                    SignUp.dao.addSignUp(getSessionAttr("userId").toString(), matchId, signName);
                    redirect("/SignUp/" + matchId + "-1");
                }
            } else {
                setAttr("matchId", matchId);
                render("SignUp.jsp");
            }
            String state = match.getStr("state");
            if(state.equals("未开始")){
                match.set("state", "报名中").update();
            }
        }

    }

    /**
     * @date 15/08/06
     * @author liuxin
     */
    public void test() {
        for (int i = 0; i < 50; i++) {
            SignUp vo = new SignUp();

            vo.set("name", "" + i + i + i + "#" + i + i + i).set("contact",
                    "" + i + i + i + "@qq.com").set("create_time", new Date()).set("userId", i + "").set("matchId",
                    "14").save();
        }

        index();
    }

    /**
     * @date 15/08/06
     * @author liuxin
     */
    @Before(SignUpValidator.class)
    public void addSignUp() {
        SignUp vo = getModel(SignUp.class, "SignUp");
        String name = vo.getStr("name").trim();
        String matchId = vo.getStr("matchId").trim();
        Match match = new Match();
        match = match.findById(matchId);
        Date beg = match.getDate("signUpTime");
        Date end = match.getDate("closingTime");
        Date now = new Date();
        if (now.after(end) || now.before(beg)) {
            renderText("报名时间未开始或已结束，感谢您的关注。");
        } else {
            List users = SignUp.dao.qrySignUpByNameAndMatch(name, matchId);

            if (users.size() > 0) {
                render("success.jsp");
            } else {
                vo.set("create_time", new Date());
                vo.set("userId", getSessionAttr("userId"));
                vo.save();
                User.dao.findById(getSessionAttr("userId")).set("signName", name).update();
                setSessionAttr("signName", name);
                redirect("/SignUp/" + matchId + "-1");
            }

        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
